<script setup lang="ts">
import { ref, computed } from "vue";
import { useColumns } from "./columns";
import Plus from "~icons/ep/plus";
import Delete from "~icons/ep/delete";
import "plus-pro-components/es/components/dialog-form/style/css";
import { addCat, setCat } from "@/api/mall";
defineOptions({
  name: "MallCat"
});
import {
  type PlusColumn,
  type FieldValues,
  PlusDialogForm
} from "plus-pro-components";
import {
  type Action,
  genFileId,
  UploadInstance,
  UploadProps,
  ElMessageBox,
  ElMessage
} from "element-plus";
import { useMenu } from "@/utils/power";
const power = useMenu(["add", "edit"]);

const edit = ref(false);

const rules = {
  name: [
    {
      required: true,
      message: "分类名称必填"
    }
  ]
};

const formColumns: PlusColumn[] = [
  {
    label: "分类名称",
    prop: "name",
    valueType: "input",
    tooltip: "请输入分类名称"
  },

  {
    label: "是否首页",
    prop: "isHome",
    valueType: "select",
    options: [
      {
        label: "否",
        value: 0
      },
      {
        label: "是",
        value: 1
      }
    ]
  },
  {
    label: "权重",
    prop: "sort",
    valueType: "input-number",
    tooltip: "请输入权重",
    props: {
      min: 0,
      precision: 0,
      step: 1
    }
  }
];
const visible = ref(false);
const values = ref<FieldValues>({ status: 0 });
const editorData = ref();
const { loading, columns, dataList, loadingConfig, adaptiveConfig, loadData } =
  useColumns();
const columnsRef = computed(() => {
  if (power.value.edit) {
    return columns;
  }
  const columnsCopy = [...columns];
  return columnsCopy.slice(0, -1);
});
const editClick = async row => {
  try {
    const params = {
      id: row.id,
      isHome: row.is_home,
      pid: row.pid,
      name: row.name,
      sort: row.sort
    };
    values.value = params;
    edit.value = true;
    visible.value = true;
    console.log("tttt", params);
  } catch (error) {
    console.error(error);
  }
};
const add = () => {
  edit.value = false;
  values.value = { isHome: 1, pid: 0, name: "", sort: 99 };
  visible.value = true;
};
const confirmLoading = ref(false);
const handleSubmit = async (value: FieldValues) => {
  const updatedValues = { ...values.value };
  confirmLoading.value = true;
  updatedValues.value = editorData.value;
  if (edit.value) {
    try {
      await setCat(updatedValues);
      visible.value = false;
      confirmLoading.value = false;
      formRef.value?.formInstance?.resetFields();
      loadData();
      ElMessage.success("修改成功");
    } catch (error) {
      confirmLoading.value = false;
      console.log(error, "error");
    }
  } else {
    try {
      await addCat(updatedValues);
      visible.value = false;
      confirmLoading.value = false;
      formRef.value?.formInstance?.resetFields();
      loadData();
    } catch (error) {
      confirmLoading.value = false;
      console.log(error, "error");
    }
  }
};
const formRef = ref();
</script>

<template>
  <ElCard>
    <template #header>
      <ElPageHeader :icon="null">
        <template #title>
          <h3>分类管理</h3>
        </template>
        <template v-if="power.add" #extra>
          <div class="flex flex-row items-end justify-end">
            <el-button type="primary" @click="add">
              <el-icon :size="15" color="#fff">
                <IconifyIconOffline :icon="Plus" />
              </el-icon>
              添加分类
            </el-button>
          </div>
        </template>
      </ElPageHeader>
    </template>
    <pure-table
      ref="tableRef"
      showOverflowTooltip
      border
      adaptive
      :adaptiveConfig="adaptiveConfig"
      row-key="id"
      alignWhole="center"
      :loading="loading"
      :loading-config="loadingConfig"
      :data="dataList"
      :columns="columnsRef"
    >
      <template #operation="{ row }">
        <el-button
          v-if="power.edit"
          type="primary"
          size="small"
          @click="editClick(row)"
          >编辑</el-button
        >
      </template>
    </pure-table>
    <PlusDialogForm
      ref="formRef"
      v-model:visible="visible"
      v-model="values"
      :form="{ columns: formColumns, rules, labelWidth: 120 }"
      :dialog="{ confirmLoading }"
      :title="edit ? '编辑配置' : '添加配置'"
      @confirm="handleSubmit"
    >
      <template #plus-field-key>
        <el-input v-model="values.key" :disabled="edit" />
      </template>
      <template #plus-field-value />
    </PlusDialogForm>
  </ElCard>
</template>

<style scoped>
::deep .wot-message-box__icon--warning {
  --el-messagebox-color: red;
  color: red !important;
  fill: red !important;
}
</style>
